package com.ry.novel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.model.dto.SysUserInfo;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.dto.condition.SysUserQuery;
import com.ry.novel.model.resp.Result;
import com.ry.novel.service.SysUserService;
import com.ry.novel.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ry
 * @since 2022-05-01 20:15
 **/
@RequestMapping("/sysUser")
@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 登录接口
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody SysUserInfo sysUserInfo) {
        sysUserInfo = sysUserService.login(sysUserInfo);

        Map<String, Object> data = new HashMap<>(2);
        data.put("token", jwtTokenUtil.generateTokenBySysUserInfo(sysUserInfo));
        data.put("userInfo", sysUserInfo);

        return Result.ok(data);
    }

    @PostMapping("/querySysUser")
    public Result<Page<SysUserInfo>> querySysUser(@RequestBody SysUserQuery sysUserQuery){
        Page<SysUserInfo> infoPage = sysUserService.querySysUser(sysUserQuery);


        return Result.ok(infoPage);
    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody SysUserInfo sysUserInfo){
        sysUserService.add(sysUserInfo);
        return Result.ok();
    }

    @PostMapping("/update")
    public Result<Void> update(@RequestBody SysUserInfo sysUserInfo){
        sysUserService.update(sysUserInfo);

        return Result.ok();
    }

    @PostMapping("/deleteById")
    public Result<Void> deleteById(@RequestParam("id") Long id){
        sysUserService.deleteById(id);

        return Result.ok();
    }
}
